﻿using Lima.ResumeSys.Class;
using Lima.ResumeSys.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Lima.ResumeSys.Models.Organization;
using static Lima.ResumeSys.Models.PerFile;

namespace Lima.ResumeSys.DAL
{
    public class OrganizationDAL
    {
        private clsDbOperate dbOperate = new clsDbOperate();
        public DataTable GetTreeViewData()
        {
            try
            {
                string sql = "select * from organization  order by Sort ";
                DataTable dt = dbOperate.dtSelectResult(sql);

                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception("获取组织结构数据发生异常：" + ex.Message);
            }
        }
        public Organization ConvertToModel(DataRow row)
        {
            Organization model = new Organization();

            if (!string.IsNullOrEmpty(row["ID"].ToString()))
            {
                model.ID = row["ID"].ToString();
            }
            if (!string.IsNullOrEmpty(row["Code"].ToString()))
            {
                model.Code = row["Code"].ToString();
            }
            if (!string.IsNullOrEmpty(row["OrgName"].ToString()))
            {
                model.OrgName = row["OrgName"].ToString();
            }
            if (!string.IsNullOrEmpty(row["Sort"].ToString()))
            {
                model.Sort = Convert.ToInt32(row["Sort"]);
            }
            if (!string.IsNullOrEmpty(row["ParCode"].ToString()))
            {
                model.ParCode = row["ParCode"].ToString();
            }
            if (!string.IsNullOrEmpty(row["IsEnabled"].ToString()))
            {
                model.IsEnabled = Convert.ToInt32(row["IsEnabled"]);
            }
            if (!string.IsNullOrEmpty(row["CreateDatetime"].ToString()))
            {
                model.CreateDatetime = Convert.ToDateTime(row["CreateDatetime"]);
            }
            if (!string.IsNullOrEmpty(row["UpDatetime"].ToString()))
            {
                model.UpDatetime = Convert.ToDateTime(row["UpDatetime"]);
                model.StrUpDatetime = model.UpDatetime.ToString("d");
            }
          
            return model;
        }
        public bool Add(Organization model)
        {
            try
            {
                string sql = @"INSERT INTO Organization(
            ID, 
            Code, 
            OrgName, 
            Sort, 
            ParCode, 
            IsEnabled, 
            CreateDatetime, 
            UpDatetime
        ) VALUES (
            @ID, 
            @Code, 
            @OrgName, 
            @Sort, 
            @ParCode,
            @IsEnabled, 
            @CreateDatetime, 
            @UpDatetime
        )";

                OleDbParameter[] parameters = {
            new OleDbParameter("@ID", model.ID),
            new OleDbParameter("@Code", model.Code),
            new OleDbParameter("@OrgName", model.OrgName),
            new OleDbParameter("@Sort", model.Sort),
            new OleDbParameter("@ParCode", model.ParCode),
            new OleDbParameter("@IsEnabled", model.IsEnabled),
            new OleDbParameter("@CreateDatetime", model.CreateDatetime),
            new OleDbParameter("@UpDatetime", model.UpDatetime)
        };

                int result = dbOperate.ExecuteNonQuery(sql, parameters);
                return result > 0;
            }
            catch
            {
                return false;
            }
        }

        public bool Update(Organization model)
        {
            try
            {
                string sql = @"UPDATE Organization SET 
            Code = @Code, 
            OrgName = @OrgName, 
            Sort = @Sort, 
            ParCode = @ParCode, 
            IsEnabled = @IsEnabled, 
            UpDatetime = @UpDatetime
        WHERE ID = @ID";

                OleDbParameter[] parameters = {
            new OleDbParameter("@Code", model.Code),
            new OleDbParameter("@OrgName", model.OrgName),
            new OleDbParameter("@Sort", model.Sort),
            new OleDbParameter("@ParCode", model.ParCode),
            new OleDbParameter("@IsEnabled", model.IsEnabled),
            new OleDbParameter("@UpDatetime", DateTime.Now.ToString()), // Or use model.UpDatetime
            new OleDbParameter("@ID", model.ID)
        };

                int result = dbOperate.ExecuteNonQuery(sql, parameters);
                return result > 0;
            }
            catch
            {
                return false;
            }
        }

        public bool Del(string id)
        {
            try
            {
                string sql = @"delete from Organization where ID=@id ";
                OleDbParameter[] parameters = { new OleDbParameter("@id", id) };
                int result = dbOperate.ExecuteNonQuery(sql, parameters);
                return result > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public Organization GetModel(string id)
        {
            try
            {
                string sql = @"select * from Organization where ID=@id ";
                OleDbParameter[] parameters = { new OleDbParameter("@id", id) };
                DataTable dt = dbOperate.ExecuteQuery(sql, parameters);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                    return ConvertToModel(row);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public List<Organization> GetList(QueryOrganizationModel query)
        {
            string strWhere = "";
            List<OleDbParameter> parameters = new List<OleDbParameter>();

            if (!string.IsNullOrWhiteSpace(query.ParCode))
            {
                strWhere += " and ParCode = @ParCode ";
                parameters.Add(new OleDbParameter("@ParCode", query.ParCode));
            }
            if (!string.IsNullOrWhiteSpace(query.OrgName))
            {
                strWhere += " and OrgName = @OrgName ";
                parameters.Add(new OleDbParameter("@OrgName", query.OrgName));
            }
            string sql = "SELECT * FROM Organization WHERE 1=1 " + strWhere +
                        " ORDER BY Sort ";
            List<Organization> list = new List<Organization>();
            //获取数据列表
            var dataTables = dbOperate.ExecuteQuery(sql, parameters.ToArray());

            if (dataTables != null && dataTables.Rows.Count > 0)
            {
                foreach (DataRow row in dataTables.Rows)
                {
                    list.Add(ConvertToModel(row));
                }
            }
            return list;
        }


    }
}
